//
//  UIViewController+THCurretnVC.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UIViewController+THCurretnVC.h"
#import "THCurrentVCWindowsManager.h"

@implementation UIViewController (THCurretnVC)

+ (UIViewController *) currentViewController
{
    UIViewController *viewController = [[THCurrentVCWindowsManager shareManager] currentWindow].rootViewController;
    return [UIViewController findBestViewController:viewController];
}

+ (UIViewController *)findBestViewController:(UIViewController *)vc
{
    if (vc.presentedViewController) {
        // Return presented view controller
        return [UIViewController findBestViewController:vc.presentedViewController];
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
        // Return right hand side
        UISplitViewController *svc = (UISplitViewController *) vc;
        if (svc.viewControllers.count > 0) {
            return [UIViewController findBestViewController:svc.viewControllers.lastObject];
        } else {
            return vc;
        }
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        // Return top view
        UINavigationController *svc = (UINavigationController *) vc;
        if (svc.viewControllers.count > 0) {
            return [UIViewController findBestViewController:svc.topViewController];
        } else {
            return vc;
        }
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        // Return visible view

        UITabBarController *svc = (UITabBarController *) vc;
        if (svc.viewControllers.count > 1) {
            return [UIViewController findBestViewController:svc.selectedViewController];
        } else {
            return vc;
        }
    } else {
        return vc;
    }
}

+ (UINavigationController *)currentNavigationController
{
    if (![[[THCurrentVCWindowsManager shareManager] currentWindow] isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    UIViewController *rootViewController = [[THCurrentVCWindowsManager shareManager] currentWindow].rootViewController;
    return [self getCurrentNaVCFrom:rootViewController];
}

+ (UINavigationController *)currentNavigationControllerWithWindow:(UIWindow *)window
{
    UIViewController *rootViewController = window.rootViewController;
    if (!rootViewController) {
        return nil;
    }
    return [self getCurrentNaVCFrom:rootViewController];
}

//递归
+ (UINavigationController *)getCurrentNaVCFrom:(UIViewController *)vc
{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
        return [self getCurrentNaVCFrom:nc];
    }
    else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentNaVCFrom:((UINavigationController *)vc).presentedViewController];
        }
        return [self getCurrentNaVCFrom:((UINavigationController *)vc).topViewController];
    }
    else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentedViewController) {
            return [self getCurrentNaVCFrom:vc.presentedViewController];
        }
        else {
            return vc.navigationController;
        }
    }
    else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}

@end
